package com.mark.common.corpus.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.ResultSetHandler;

import com.mark.common.util.jdbc.DaoBase;
import com.mark.common.util.jdbc.DataSourceFactory;

public class MessageTextDao extends DaoBase {
	
	public MessageTextDao(){
		this.setDataSource(new DataSourceFactory().getDataSource());
	}
	
	public List<String> getWBMessageText( int startNo,int limitNo ) throws Exception{
		List<String> tempLS = new ArrayList<String>();
		String sqlSTR = "select text from wb_message limit "+startNo+","+limitNo;
		ResultSetHandler <List<String>> cQueryHandler = new ResultSetHandler< List<String>>(){
			//@Override
			public List<String> handle(ResultSet rs) throws SQLException {
				List<String> tempList = new ArrayList<String>();
				while(rs.next()) {
					//System.out.println(rs.getString(1));
					tempList.add(rs.getString(1));
				}
				return tempList;
			}
		};
		tempLS = this.findBeans(sqlSTR, cQueryHandler);
		return tempLS;
	}
	
	public List<String> getTXMessageText( int startNo,int limitNo ) throws Exception{
		List<String> tempLS = new ArrayList<String>();
		String sqlSTR = "select text from tx_message limit "+startNo+","+limitNo;
		//String sqlSTR = "select text from tx_message";
		ResultSetHandler <List<String>> cQueryHandler = new ResultSetHandler< List<String>>(){
			//@Override
			public List<String> handle(ResultSet rs) throws SQLException {
				List<String> tempList = new ArrayList<String>();
				while(rs.next()) {
					tempList.add(rs.getString(1));
				}
				return tempList;
			}
		};
		tempLS = this.findBeans(sqlSTR, cQueryHandler);
		return tempLS;
	}
	
	public int getWBMessageCount() throws Exception {
		Integer tempCountNo = 0;
		String sqlSTR = "select count(*) from wb_message";
		ResultSetHandler <Integer> rQueryHandler = new ResultSetHandler<Integer>() {
			//@Override
			public Integer handle(ResultSet re) throws SQLException{
				while(!re.next()){
					return 0;
				}
				return re.getInt(1);
			}
		};
		tempCountNo = this.findBean(sqlSTR, rQueryHandler);
		return tempCountNo;
	}
	
	public int getTXMessageCount() throws Exception {
		Integer tempCountNo = 0;
		String sqlSTR = "select count(*) from tx_message";
		ResultSetHandler <Integer> rQueryHandler = new ResultSetHandler<Integer>() {
			//@Override
			public Integer handle(ResultSet re) throws SQLException{
				while(!re.next()){
					return 0;
				}
				return re.getInt(1);
			}
		};
		tempCountNo = this.findBean(sqlSTR, rQueryHandler);
		return tempCountNo;
	}

}
